import { getToken, setToken, removeToken } from "@/common/token";
import { login, logout, clear } from "@/api/login";
import { getInfo } from "@/api/system/member";
import { getSetter } from "@/utils";

const user = {
    state: {
        token: getToken(),
        userInfo: {
            name: "",
            roleName: "",
            nickName: "",
            avatar: ""
        },
        perms: []
    },
    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token;
        },
        SET_PERMISSON: (state, perms) => {
            state.perms = perms;
        },
        setUserInfo: getSetter("userInfo")
    },
    actions: {
        GetInfo({ commit, state }) {
            return new Promise((resolve, reject) => {
                getInfo(state.token)
                    .then(res => {
                        const user = res.data.userInfo;
                        const avatar =
                            user.face == null
                                ? require("@/assets/img/empty-face.png")
                                : process.env.VUE_APP_BASE_API + user.face;
                        commit("SET_PERMISSON", res.data.perms);
                        commit("setUserInfo", { ...user, avatar });
                        resolve(res);
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
        Login({ commit }, userInfo) {
            return new Promise((resolve, reject) => {
                login(userInfo)
                    .then(res => {
                        console.log("member_login", res);
                        setToken(res.data);
                        commit("SET_TOKEN", res.data);
                        resolve();
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
        LoginOut({ commit }) {
            return new Promise((resolve, reject) => {
                logout()
                    .then(res => {
                        console.log("member_logout", res);
                        commit("SET_TOKEN", "");
                        commit("SET_PERMISSON", []);
                        removeToken();
                        resolve();
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
        Clear({ commit }) {
            return new Promise((resolve, reject) => {
                clear()
                    .then(res => {
                        console.log("member_clear", res);
                        commit("SET_TOKEN", "");
                        commit("SET_PERMISSON", []);
                        removeToken();
                        resolve();
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        }
    }
};
export default user;
